package cn.dxl.base.controller;


import cn.dxl.base.pojo.FragmentUpload.MultipartFileParam;
import cn.dxl.base.service.FragmentUploadService.FragmentUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(value = "分片上传")
@Slf4j
@RestController
@RequestMapping(value = "/fragmentUploadClass")
public class FragmentUploadController {


    @Autowired
    private FragmentUploadService fragmentUploadService;


    @ApiOperation("大文件分片上传")
    @PostMapping("chunkUpload")
    public void fileChunkUpload(MultipartFileParam param, HttpServletResponse response, HttpServletRequest request) {
        /**
         * 判断前端Form表单格式是否支持文件上传
         */
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
            log .info("isMultipart----异常~~~~~~~~~~~~~~~~~~~");
            return;
        }
        log.info("上传文件 start...");
        try {
            String taskId = fragmentUploadService.chunkUploadByMappedByteBuffer(param);
        } catch (IOException e) {
            log.error("文件上传失败。{}", param.toString());
        }
        log.info("上传文件结束");
    }

}
